<html>
<head>
<script>
function keep_two() {
  chrome.windows.getAll({populate: true}, function(windows){
    var tab_count = 0;
    for (var i=0; i<windows.length; i++) {
      var w = windows[i];
      tab_count += w.tabs.length;
      };
    if (tab_count == 1) {
      var select_new_created = localStorage["select_new_created"];
      select_new_created = (select_new_created == 1);
      chrome.tabs.create({selected: select_new_created});
      }
    });
  }
function close_second(tab) {
  chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
    if (tabs.length<3)
      return;
    for (var i=0, closed=0; i<tabs.length && closed < tabs.length-2; i++) {
      if (tabs[i].id == tab.id) // Do not close new tab which is chrome://newtab
        continue;
      if (tabs[i].url == 'chrome://newtab/') {
        chrome.tabs.remove(tabs[i].id);
        closed++;
        }
      }
    });
  }
keep_two();
chrome.tabs.onRemoved.addListener(function(tabId) {
  keep_two()
  });
chrome.tabs.onCreated.addListener(function(tab) {
  var close_second_tab = localStorage["close_second_tab"];
  close_second_tab = (close_second_tab == 1);
  if (close_second_tab)
    close_second(tab);
  });
</script>
</head>
</html>
<!-- vim: set sw=2 ts=2 et: -->
